//
//  WBUserAccountModel.swift
//  微博(swift)
//
//  Created by 汤米 on 16/4/19.
//  Copyright © 2016年 汤米. All rights reserved.
//

import UIKit

class WBUserAccountModel: NSObject,NSCoding {

    //用户授权的唯一票据
    var access_token: String?
    
    //access_token的生命周期，单位是秒数。
    var expires_in: Int = 0 {
        didSet {
            
            expires_date = NSDate().dateByAddingTimeInterval(Double(expires_in))

        }
    }
    
    var remind_in: String?
    
    //授权用户的UID
    var uid: String?
    
    //用户昵称
    var screen_name: String?
    
    //用户头像
    var avatar_large: String?
    
    //便于开发时判断access_token是否过期
    var expires_date: NSDate?
    
    // MARK: -  归档
    func encodeWithCoder(aCoder: NSCoder) {
        
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(uid, forKey: "uid")
    }
    
    // MARK: -  解档
    required init?(coder aDecoder: NSCoder) {
        
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String

    }
  
    // MARK: -  KVC赋值
    init(dict: [String: AnyObject]) {
        
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    // MARK: -  空实现
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    // MARK: -  重写description属性
    override var description: String {
        let keys = ["access_token","expires_in","remind_in","uid","screen_name","avatar_large"]
        return dictionaryWithValuesForKeys(keys).description
    }
}
